package LiAN;

import com.xinzhi.JDBCUtils.JDBCUtils;
import com.xinzhi.entity.User;

import javax.xml.transform.Result;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Lian implements ILian{
    @Override
    public int addUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int i = -1;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "insert into t_dept values(0,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getAddress());
            i = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public int deleteUser(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int i = -1;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","123456");
            String sql = "delete from user where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            i = preparedStatement.executeUpdate();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (preparedStatement != null)
                    preparedStatement.close();
                if (connection != null);
                    connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return i;
    }

    @Override
    public int updateUser(User user) {
        Connection connection = null;
        Statement statement = null;
        int i = -1;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","123456");
            String sql = "update user set";
            int num = -1;
            if (user.getUsername() != null){
                sql +="name ='" + user.getUsername()+"'";
                num = 1;
            }
            System.out.println(sql);
            statement = connection.createStatement();
            i = statement.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return i;
    }

    @Override
    public List<User> getAllUser() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        List<User> list = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","123456");
            String sql = "select * from user";
            System.out.println(sql);
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            list = new ArrayList<>();
            while (resultSet.next()){
                user = new User();

            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {

                try {
                    if (preparedStatement != null)
                        preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        return list;


    }

}

